package com.smart.city.logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.widget.Toast;

public class FileLogger implements Loggable {
	private String logFileName  = null; 
	private FileOutputStream fos;
	
	public FileLogger(String fileName) {
		this.logFileName = fileName;
	}
	
	public void close() {
		if (this.fos != null) {
			try {
				this.fos.flush();
				this.fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void open() {
		File logFile = new File(this.logFileName);
		try {
			logFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		try {	
			if ((this.fos = new FileOutputStream(logFile)) == null) {
				Toast.makeText(null, "xxx", Toast.LENGTH_SHORT).show();
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}

	public void write(String msg) {
		if (this.fos != null) {
			try {
				this.fos.write((msg + "\n").getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
